/**
 * @Date 2023-02-21 14:00:07
 * @Remark
 */

// lib
// import moment from "moment";
// config
// script & methods & public
import toast from "components/toast";
// import timeUtils from "utils/timeUtils";
// store
import { state } from "./state";
// service
import {
  addSourceService,
  deleteSourceService,
  editSourceService,
  getSourcePageService,
} from "biz/Books/service";
import { ReqSourceAddPage, ReqSourcePage } from "biz/Books/api";
// interface

export async function getSourcePage() {
  const sp = state.getSearch();
  const params: ReqSourcePage = {
    pageNum: state.page,
    pageSize: state.size,
    ...sp,
  };
  state.loading = true;
  const result = await getSourcePageService(params);
  state.loading = false;
  if (result.payload) {
    state.total = result.payload.total;
    state.list = result.payload.list;
  }
}

export async function addSource(form: any) {
  const params: ReqSourceAddPage = {
    ...form,
  };
  state.editModalLoading = true;
  const result = await addSourceService(params);
  state.editModalLoading = false;
  if (result.payload) {
    toast.success("添加成功");
    state.editModal = false;
    getSourcePage();
  }
}

export async function updateSource(form: any, sourceId: number) {

  // 调用服务
  state.editModalLoading = true;
  const result = await editSourceService({
    sourceId,
    ...form,
  });
  state.editModalLoading = false;
  // 成功
  if (result.payload) {
    toast.success("编辑成功");
    state.editModal = false;
    getSourcePage();
  }
}
// //不分页列表
// export async function getSourceLongPage() {
//   state.loading = true;
//   const result = await getSourceLongPageService({});
//   state.loading = false;
//   if (result.payload) {
//     state.longList = result.payload.filter(
//       (item: any) => item.parentId === "0",
//     );
//   }
// }

export async function delSource(id: number) {
  const result = await deleteSourceService({ sourceId: id });
  if (result.payload) {
    toast.success("删除成功");
    state.setSearch();
    getSourcePage();
  }
}

export async function didMount() {
  getSourcePage();
  // getProSourceLongPage();
}
